﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;

namespace HoaDon
{
	public partial class ChangeUserInfo : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				wrnWrongPassword.InnerText = "";
				SucsessChangePassword.InnerText = "";

				if (!IsPostBack)
				{
					LoadInfomation();
				}
			}
			catch
			{
				Response.Redirect("~/Error.aspx", false);
			}
		}

		private void LoadInfomation()
		{
			SqlConnection proxyConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString);
			proxyConn.Open();

			SqlCommand sqlComm = new SqlCommand("SELECT * FROM userinfo WHERE UPPER(UserID) = @UserID", proxyConn);
			sqlComm.CommandType = CommandType.Text;
			sqlComm.Parameters.Add("@UserID", SqlDbType.VarChar).Value = User.Identity.Name.ToUpper();
			SqlDataAdapter sqlDA = new SqlDataAdapter();
			sqlDA.SelectCommand = sqlComm;
			DataTable dtUser = new DataTable();
			sqlDA.Fill(dtUser);

			if (dtUser.Rows.Count > 0)
			{
				txtAgentName.Text = dtUser.Rows[0]["UserName"].ToString();
				txtPhone.Text = dtUser.Rows[0]["Phone"].ToString();
				txtCompany.Text = dtUser.Rows[0]["Company"].ToString();
				txtAddress.Text = dtUser.Rows[0]["Address"].ToString();
			}
			proxyConn.Close();
			proxyConn.Dispose();

			if (CommonFunction.GetReportType("HDA") == "0" || CommonFunction.GetReportType("HDA") == "1")
			{
				rb80.Checked = true;
			}
			else
			{
				rbA5.Checked = true;
			}
		}

		private string GetPassword(string userid)
		{
			SqlConnection proxyConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString);
			proxyConn.Open();

			SqlCommand sqlComm = new SqlCommand("SELECT Password FROM userinfo WHERE UPPER(UserID) = @UserID", proxyConn);
			sqlComm.CommandType = CommandType.Text;
			sqlComm.Parameters.Add("@UserID", SqlDbType.VarChar).Value = userid.ToUpper();
			SqlDataAdapter sqlDA = new SqlDataAdapter();
			sqlDA.SelectCommand = sqlComm;
			DataTable dtUser = new DataTable();
			sqlDA.Fill(dtUser);

			proxyConn.Close();
			proxyConn.Dispose();

			if (dtUser.Rows.Count > 0)
			{
				return dtUser.Rows[0]["Password"].ToString();
			}
			else
				return "";
		}

		protected void btnOk_Click(object sender, EventArgs e)
		{
			try
			{
				if (User.Identity.Name.ToUpper() == "DEMO001")
				{
					SucsessChangePassword.InnerText = "Đây là tài khoản dùng thử, không được thay đổi thông tin";
					return;
				}
				if (Enc.Encrypt(txtOldPassword.Text, User.Identity.Name.ToUpper()) == GetPassword(User.Identity.Name))
				{
					SqlConnection proxyConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString);
					proxyConn.Open();

					string sCom = "UPDATE userinfo SET ";
					if (txtPassword.Text.Trim().Length > 0)
					{
						sCom = sCom + "Password = @Password, ";
					}
					sCom = sCom + "UserName = @UserName, ";
					sCom = sCom + "Phone = @Phone, ";
					sCom = sCom + "Company = @Company, ";
					sCom = sCom + "Address = @Address ";
					sCom = sCom + "WHERE UPPER(UserID) = @UserID";

					SqlCommand sqlComm = new SqlCommand(sCom, proxyConn);
					sqlComm.CommandType = CommandType.Text;
					if (txtPassword.Text.Trim().Length > 0)
					{
						sqlComm.Parameters.Add("@Password", SqlDbType.VarChar).Value = Enc.Encrypt(txtPassword.Text.Trim(), User.Identity.Name.ToUpper());
					}
					sqlComm.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtAgentName.Text;
					sqlComm.Parameters.Add("@Phone", SqlDbType.VarChar).Value = txtPhone.Text;
					sqlComm.Parameters.Add("@Company", SqlDbType.NVarChar).Value = txtCompany.Text;
					sqlComm.Parameters.Add("@Address", SqlDbType.NVarChar).Value = txtAddress.Text;
					sqlComm.Parameters.Add("@UserID", SqlDbType.VarChar).Value = User.Identity.Name.ToUpper();
					if (sqlComm.ExecuteNonQuery() > 0)
					{
						SucsessChangePassword.InnerText = "Lưu thông tin thay đổi thành công!";
					}
					proxyConn.Close();
					proxyConn.Dispose();

					if (rb80.Checked == true)
					{
						CommonFunction.SetReportType("HDA", "1");
					}
					else
					{
						CommonFunction.SetReportType("HDA", "2");
					}
				}
				else
				{
					wrnWrongPassword.InnerText = "  Sai mật khẩu";
				}
			}
			catch
			{
				Response.Redirect("~/Error.aspx", false);
			}
		}
	}
}
